package com.ky.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ky.tms.entity.TmsExpense;
import com.ky.tms.enums.ExpenseEnum;
import com.ky.tms.enums.RoleEnum;
import com.ky.tms.mapper.TmsExpenseMapper;
import com.ky.tms.service.ITmsExpenseService;
import com.ky.tms.util.DateUtil;
import com.ky.tms.util.RequestContextHolderUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2020-05-07
 */
@Service
public class TmsExpenseServiceImpl extends ServiceImpl<TmsExpenseMapper, TmsExpense> implements ITmsExpenseService {

    @Autowired
    private TmsExpenseMapper tmsExpenseMapper;

    @Override
    public IPage<TmsExpense> page(TmsExpense tmsExpense, Long limit, Long pages) {
        String userName = RequestContextHolderUtil.getUserName();
        String role = RequestContextHolderUtil.getRole();
        IPage<TmsExpense> page = new Page(pages, limit);
        QueryWrapper<TmsExpense> wrapper = new QueryWrapper<>();
        if(ExpenseEnum.ENTRY.getValue().equals(tmsExpense.getFlag())){
            wrapper.eq("create_user", userName);
        }
        if(ExpenseEnum.APPLY.getValue().equals(tmsExpense.getFlag())){
            wrapper.in("status", new String[]{ExpenseEnum.APPLY.getValue(), ExpenseEnum.ENTRY.getValue(), ExpenseEnum.PASS.getValue()});
        }
        if(ExpenseEnum.PASS.getValue().equals(tmsExpense.getFlag())){
            wrapper.in("status", new String[]{ExpenseEnum.APPLY.getValue(), ExpenseEnum.ENTRY.getValue(), ExpenseEnum.PASS.getValue(), ExpenseEnum.LEADER_PASS.getValue()});
        }
        if(ExpenseEnum.LEADER_PASS.getValue().equals(tmsExpense.getFlag())){
            // wrapper.in("status", new String[]{ExpenseEnum.APPLY.getValue(), ExpenseEnum.ENTRY.getValue(), ExpenseEnum.PASS.getValue(), ExpenseEnum.LEADER_PASS.getValue(), ExpenseEnum.PAY_ALLOW.getValue()});
        }
        if(ExpenseEnum.PAY.getValue().equals(tmsExpense.getFlag())){
            wrapper.in("status", new String[]{ExpenseEnum.PAY_ALLOW.getValue(),ExpenseEnum.PAY.getValue()});
        }
        if(StringUtils.isNotEmpty(tmsExpense.getExpSummary())){
            wrapper.like("exp_summary", "%" + tmsExpense.getExpSummary() + "%");
        }
        if(StringUtils.isNotEmpty((tmsExpense.getStatus()))){
            wrapper.eq("status", tmsExpense.getStatus());
        }
        if(StringUtils.isNotEmpty(tmsExpense.getExpMan())){
            wrapper.like("exp_man", "%"+ tmsExpense.getExpMan() + "%");
        }
        if(StringUtils.isNotEmpty(tmsExpense.getExpTimeStr())){
            wrapper.gt("exp_time", DateUtil.strToLocalTime(tmsExpense.getExpTimeStr(),"yyyy-MM-dd HH:mm:ss"));

        }
        if(StringUtils.isNotEmpty(tmsExpense.getExpTimeEnd())){
            wrapper.lt("exp_time", DateUtil.strToLocalTime(tmsExpense.getExpTimeEnd(),"yyyy-MM-dd HH:mm:ss"));

        }
        wrapper.eq("is_valid", 1);
        wrapper.orderByDesc("create_time");
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public void add( TmsExpense tmsExpense) {
        String userName = RequestContextHolderUtil.getUserName();
        tmsExpense.setIsValid(1);
        tmsExpense.setCreateUser(userName);
        tmsExpense.setCreateTime(LocalDateTime.now());
        tmsExpenseMapper.insert(tmsExpense);
    }

    @Override
    public void delete(List<Long> ids) {
        String userName = RequestContextHolderUtil.getUserName();
        QueryWrapper<TmsExpense> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        TmsExpense tmsExpense = new TmsExpense();
        tmsExpense.setUpdateUser(userName);
        tmsExpense.setUpdateTime(LocalDateTime.now());
        tmsExpense.setIsValid(0);
        tmsExpenseMapper.update(tmsExpense, wrapper);

    }

    @Override
    public void updateStatus(List<Long> ids, String status) {
        String userName = RequestContextHolderUtil.getUserName();
        QueryWrapper<TmsExpense> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        TmsExpense tmsExpense = new TmsExpense();
        tmsExpense.setUpdateUser(userName);
        tmsExpense.setUpdateTime(LocalDateTime.now());
        tmsExpense.setStatus(status);
        tmsExpenseMapper.update(tmsExpense, wrapper);
    }

    @Override
    public void update(TmsExpense tmsExpense) {
        String userName = RequestContextHolderUtil.getUserName();
        tmsExpense.setUpdateUser(userName);
        tmsExpense.setUpdateTime(LocalDateTime.now());
        tmsExpenseMapper.updateById(tmsExpense);

    }
}
